$(function () {
    getUserInfo() 
    function getUserInfo () {
        $.ajax({
            method: 'get',
            url: '/my.userinfo',
            // headers: {
            //     Authorization: localStirage.getItem('token')
            // },
            success(res) {
                if (res.status !== 0) {
                    return layui.layer.msg(res.message, { icon: 5 })
                }
                renderAvatar(res.data);
            },
        
        })
    }
    function renderAvatar (user) {
        console.log(user) 
            var name = user.nickname ||user.username
            console.log(name)
            $('#welcome').html('欢迎' + name)
        
    }
    function renderAvatar(user) {
        console.log(user)
        var name = user.nickname||user.username
        $('#welcome').html('欢迎' + name) 
        if(user.user_pic !== null) {
            $('.layui-nav-img').show().attr('src',user.user_pic)
            $('.text-avatar').hide()
        } else {
            $('.layui-nav-img').hide()
            var first = name[0].toUpperCase()
            $('.text-avatar').show().html(first)
        }
    }
    $('#logout').on('click',function () {
        // 询问
        // var flag = confirm('您确定要退出吗？')
        // console.log(flag);
        // if (confirm('您确定要退出么')) {
        //     localStorage.removeItem("token")
        //     location.href = 'login.html'
        layui.layer.confirm('您确定要退出吗?', {icon: 3, title:'提示'}, function(index){
            //do something
            localStorage.removeItem("token")
            location.href = 'login.html'
            layui.layer.close(index);
          });
    })
})